home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / SORTING.SWG / 0016_QUICK2.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  2KB  |  70 lines

  1. {...This is as generic a QuickSort as I currently use:
  2. }
  3. {$A+,B-,D-,E-,F-,G-,I-,L-,N-,O-,P-,R-,S-,T-,V-}
  4. {$M 60000,0,0}
  5.  
  6. Program QuickSortDemo;
  7. Uses
  8.   Crt;
  9.  
  10. Const
  11.   coMaxItem = 30000;
  12.  
  13. Type
  14.   Item   = Word;
  15.   arItem = Array[1..coMaxItem] of Item;
  16.  
  17.   (***** QuickSort routine.                                           *)
  18.   (*                                                                  *)
  19. Procedure QuickSort({update} Var arData  : arItem;
  20.                       {input }     woLeft,
  21.                                    woRight : Word);
  22. Var
  23.   Pivot,
  24.   TempItem : Item;
  25.   woIndex1,
  26.   woIndex2 : Word;
  27. begin
  28.   woIndex1 := woLeft;
  29.   woIndex2 := woRight;
  30.   Pivot := arData[(woLeft + woRight) div 2];
  31.   Repeat
  32.     While (arData[woIndex1] < Pivot) do
  33.       inc(woIndex1);
  34.     While (Pivot < arData[woIndex2]) do
  35.       dec(woIndex2);
  36.     if (woIndex1 <= woIndex2) then
  37.       begin
  38.         TempItem := arData[woIndex1];
  39.         arData[woIndex1] := arData[woIndex2];
  40.         arData[woIndex2] := TempItem;
  41.         inc(woIndex1);
  42.         dec(woIndex2)
  43.       end
  44.     Until (woIndex1 > woIndex2);
  45.     if (woLeft < woIndex2) then
  46.       QuickSort(arData, woLeft, woIndex2);
  47.     if (woIndex1 < woRight) then
  48.       QuickSort(arData, woIndex1, woRight)
  49. end;        (* QuickSort.                                           *)
  50.  
  51. Var
  52.   woIndex : Word;
  53.   Buffer  : arItem;
  54.  
  55. begin
  56.   Write('Creating ', coMaxItem, ' random numbers... ');
  57.   For woIndex := 1 to coMaxItem do
  58.     Buffer[woIndex] := random(65535);
  59.   Writeln('Finished!');
  60.   Write('Sorting  ', coMaxItem, ' random numbers... ');
  61.   QuickSort(Buffer, 1, coMaxItem);
  62.   Writeln('Finished!');
  63.   Writeln;
  64.   Writeln('Press the <ENTER> key to display all ', coMaxItem,
  65.           ' sorted numbers...');
  66.   readln;
  67.   For woIndex := 1 to coMaxItem do
  68.     Write(Buffer[woIndex]:8)
  69. end.
  70.